#include "dialogpwd.h"
#include "ui_dialogpwd.h"
#include "qthelper.h"

DialogPwd::DialogPwd(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogPwd)
{
    ui->setupUi(this);
    this->initForm();
    this->initConfig();
}

DialogPwd::~DialogPwd()
{
    delete ui;
}

void DialogPwd::setOldPwd(QString strPwd)
{
    m_strOldPwd = strPwd;
}

QString DialogPwd::getPwd()
{
    return m_strNewPwd;
}

void DialogPwd::initForm()
{
    Qt::WindowFlags flags = this->windowFlags();
    flags &= ~Qt::WindowContextHelpButtonHint;
    this->setWindowFlags(flags);

    this->setWindowTitle("口令修改");
}

void DialogPwd::initConfig()
{
    connect(ui->pushButtonSave, &QPushButton::clicked, this, &DialogPwd::onButtonClickedSave);
    connect(ui->pushButtonCanel, &QPushButton::clicked, this, &DialogPwd::reject);

}

void DialogPwd::onButtonClickedSave()
{
    // 执行保存操作
    QString  strOld = ui->lineEdit_old->text();
    if (strOld.isEmpty()) {
        QtHelper::showMessageBoxError("请输入旧口令!");
        ui->lineEdit_old->setFocus();
        return;
    }

    QString  strNew = ui->lineEdit_new->text();
    if (strNew.isEmpty()) {
        QtHelper::showMessageBoxError("请输入新口令!");
        ui->lineEdit_new->setFocus();
        return;
    }

    QString  strVerify = ui->lineEdit_verify->text();
    if (strVerify.isEmpty()) {
        QtHelper::showMessageBoxError("请输入确认口令!");
        ui->lineEdit_verify->setFocus();
        return;
    }

    if(strNew != strVerify)
    {
        QtHelper::showMessageBoxError("新口令与确认口令不一致,请检查!");
        ui->lineEdit_verify->setFocus();
        return;
    }

    if(strOld != m_strOldPwd)
    {
        QtHelper::showMessageBoxError("旧口令错误,请检查!");
        ui->lineEdit_old->setFocus();
        return;
    }

    m_strNewPwd = strNew;

    // 关闭对话框
    accept();
}


